速報APP / 教育 / Graham Scan

Graham Scan

價格:免費

更新日期:2018-04-14

檔案大小:2.6M

目前版本:1.0

版本需求:Android 4.2 以上版本

官方網站:mailto:tanishqsaluja18@gmail.com

Graham Scan(圖1)-速報App

The first step in this algorithm is to find the point with the lowest y-coordinate. If the lowest y-coordinate exists in more than one point in the set, the point with the lowest x-coordinate out of the candidates should be chosen. Call this point P. This step takes O(n), where n is the number of points in question.

Graham Scan(圖2)-速報App

Next, the set of points must be sorted in increasing order of the angle they and the point P make with the x-axis. Any general-purpose sorting algorithm is appropriate for this, for example heapsort (which is O(n log n)).

Graham Scan(圖3)-速報App

The algorithm proceeds by considering each of the points in the sorted array in sequence. For each point, it is first determined whether traveling from the two points immediately preceding this point constitutes making a left turn or a right turn. If a right turn, the second-to-last point is not part of the convex hull, and lies 'inside' it.

Graham Scan(圖4)-速報App

Sorting the points has time complexity O(n log n). While it may seem that the time complexity of the loop is O(n2), because for each point it goes back to check if any of the previous points make a "right turn", it is actually O(n), because each point is considered at most twice in some sense. Each point can appear only once as a point ( x 2 , y 2 ) in a "left turn" (because the algorithm advances to the next point ( x 3 , y 3 )after that), and as a point ( x 2 , y 2 ) in a "right turn" (because the point ( x 2 , y 2 ) { is removed). The overall time complexity is therefore O(n log n), since the time to sort dominates the time to actually compute the convex hull.

Graham Scan(圖5)-速報App